package com.yunsoft.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by yangyunfeng on 2017/2/23.
 */

public class DimenTool {



    public static void gen() {
    	String profilePath = "/Users/yangyunfeng/Documents/workspace/ielvb-android";
        File file = new File(profilePath+"/app/src/main/res/values/dimens_values.xml");
        BufferedReader reader = null;
        StringBuilder sw480 = new StringBuilder();
        StringBuilder sw600 = new StringBuilder();
        StringBuilder sw720 = new StringBuilder();
//        StringBuilder sw800 = new StringBuilder();
        StringBuilder w820 = new StringBuilder();
        try {
            System.out.println("生成不同分辨率：");
            reader = new BufferedReader(new FileReader(file));
            String tempString;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束

            while ((tempString = reader.readLine()) != null) {

                if (tempString.contains("</dimen>")) {
                    //tempString = tempString.replaceAll(' ', '');
                    String start = tempString.substring(0, tempString.indexOf(">") + 1);
                    String end = tempString.substring(tempString.lastIndexOf("<") - 2);
                    int num = Integer.valueOf(tempString.substring(tempString.indexOf(">") + 1, tempString.indexOf("</dimen>") - 2));

                    sw480.append(start).append((int) Math.round(num * 0.66)).append(end).append("");
                    sw600.append(start).append((int) Math.round(num * 0.83)).append(end).append("");
                    sw720.append(tempString).append("");
                    w820.append(start).append((int) Math.round(num * 1.12)).append(end).append("");
             

                } else {
                    sw480.append(tempString).append("");
                    sw600.append(tempString).append("");
                    sw720.append(tempString).append("");
                    w820.append(tempString).append("");
//                    w820.append(tempString).append("");
                }
                line++;
            }
            reader.close();
           System.out.println("<!--  sw480 -->");
            System.out.println(sw480);
            System.out.println("<!--  sw600 -->");
            System.out.println(sw600);

            System.out.println("<!--  sw720 -->");
            System.out.println(sw720);
            System.out.println("<!--  w820 -->");
            System.out.println(w820);

            String sw480file = profilePath+"/app/src/main/res/values-sw480dp-land/dimens.xml";
            String sw600file =profilePath+ "/app/src/main/res/values-sw600dp-land/dimens.xml";
            String sw720file =profilePath+ "/app/src/main/res/values-sw720dp-land/dimens.xml";
            String sw800file = profilePath+"/app/src/main/res/values-sw800dp-land/dimens.xml";
          
            writeFile(sw480file, sw480.toString());
            writeFile(sw600file, sw600.toString());
            writeFile(sw720file, sw720.toString());
            writeFile(sw800file, w820.toString());
           
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void writeFile(String file, String text) {
        PrintWriter out = null;
        try {
            out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
            out.println(text);
        } catch (IOException e) {
            e.printStackTrace();
        }

        out.close();
    }

    public static void main(String[] args) throws Exception{
		System.out.println("ddddd");
	}
//    public static void main(String[] args) {
//     //   gen();
//    }
}
